package com.example.activity;

import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.Base.BasePersonActivity;
import com.example.administrator.olddoctor.R;
import com.example.tools.StringUtils;

import static com.example.Base.BaseContent.EDITPERSONAL_NIKENAME_RESULTCODE;
import static com.example.Base.BaseContent.NICKNAME;
import static com.example.Base.BaseContent.NULL;

/**
 * 编辑的下一个
 */
public class EditPersonalDetailActivity extends BasePersonActivity implements TextWatcher,View.OnClickListener {

    private EditText text;
    private ImageView delete;
    private Button ok_button_personal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_personal_detail);
        init();
        initBundle();
    }

    private void init() {
        setActionBar(getResources().getDrawable(R.mipmap.call_back), null,"修改昵称",null,null,null);
        text = (EditText) findViewById(R.id.text);delete = (ImageView) findViewById(R.id.delete);
        ok_button_personal = (Button) findViewById(R.id.ok_button_personal);
        ok_button_personal.setOnClickListener(this);
        text.setFocusable(true);text.setFocusableInTouchMode(true);
        text.addTextChangedListener(this);delete.setOnClickListener(this);
        getActionBarRightText().setOnClickListener(this);getActionBarLeft().setOnClickListener(this);
        text.setOnClickListener(this);
    }

    public void initBundle() {
        String name = getIntent().getExtras().getString(NICKNAME);
        if(!NULL.equals(name)){
            text.setText(name);
        }
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.actionbar_right_text:

                break;
            case R.id.actionbar_left:
                showToast("点击了返回");
                break;
            case R.id.delete:
                text.setText("");
                delete.setVisibility(View.INVISIBLE);
                break;
            case R.id.ok_button_personal:
                String nickname = text.getText().toString();
                if(StringUtils.isSpace(nickname)){
                    showToast("您不能输入空名字");
                } else if(nickname.trim().length()<2){
                    showToast("您的名字要两个字以上哦~");
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString(NICKNAME,nickname);
                    setResult(EDITPERSONAL_NIKENAME_RESULTCODE,new Intent().putExtras(bundle));
                    finish();
                }
                break;
            case R.id.text:

                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(charSequence.length()>0){
            delete.setVisibility(View.VISIBLE);
            ok_button_personal.setEnabled(true);
        } else {
            delete.setVisibility(View.INVISIBLE);
            ok_button_personal.setEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}
